// pages/my/my.js
Page({
  data: {
    isLoggedIn: false,
    name: "",
    avatar: "",
    integral: "",
  },

  checkLoginStatus() {
    if (!this.data.isLoggedIn) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        confirmText: '去登录',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            this.toLogon();
          }
        }
      });
      return false;
    }
    return true;
  },

  tobindphone() {
    if (this.checkLoginStatus()) {
      wx.navigateTo({
        url: '/pages/register/bindphone1/bindphone1',
      })
    }
  },

  toCourseCollection() {
    if (this.checkLoginStatus()) {
      wx.navigateTo({
        url: '/pages/course/courseCollection/courseCollection',
      })
    }

  },

  toMyNotify() {
    if (this.checkLoginStatus()) {
      wx.navigateTo({
        url: '/pages/my/mynotify/mynotify',
      })
    }
  },


  toAbout() {
    wx.navigateTo({
      url: '/pages/about/about',
    })
  },

  toMyAssessment() {
    if (this.checkLoginStatus()) {
      wx.navigateTo({
        url: '/pages/my/myassessment/myassessment',
      })
    }
  },

  toMyanswer() {
    if (this.checkLoginStatus()) {
      wx.navigateTo({
        url: '/pages/my/myanswer/myanswer',
      })
    }
  },

  toMyconsult() {
    if (this.checkLoginStatus()) {
      wx.navigateTo({
        url: '/pages/my/myconsult/myconsult',
      })
    }
  },

  toLogon() {
    wx.navigateTo({
      url: '/pages/logon/logon',
    })
  },

  exit() {
    wx.removeStorage({
      key: 'userId',
    })
    wx.removeStorage({
      key: 'username',
    })
    wx.removeStorage({
      key: 'avatar',
    })
    this.setData({
      isLoggedIn: false,
      name: "",
      avatar: ""
    });
    wx.showToast({
      title: '已退出登录',
      icon: 'success'
    });
  },

  getUserInfo() {
    const userId = wx.getStorageSync('userId');
    const username = wx.getStorageSync('username');
    const phone = wx.getStorageSync('phone');
    const avatar = wx.getStorageSync('avatar');
  
    console.log('Retrieved user info:', { userId, username, phone, avatar });
  
    if (userId) {
      this.setData({
        isLoggedIn: true,
        name: username,
        phone: phone,
        avatar: avatar
      });
      console.log('User is logged in, set data:', this.data);

    } else {
      this.setData({
        isLoggedIn: false,
        name: "",
        avatar: ""
      });
    }
  },

  onLoad(options) {
    console.log('My page onLoad');
    this.getUserInfo();
  },

  onTabItemTap(item) {
    console.log('Tab item tapped:', item);
    this.getUserInfo();
  },
  //检查缓存
  checkStoredData() {
    const userId = wx.getStorageSync('userId');
    const username = wx.getStorageSync('username');
    const avatar = wx.getStorageSync('avatar');
    console.log('Stored data:', { userId, username, avatar });
  },


  onShow() {
    console.log('My page onShow');
    this.checkStoredData();
    this.getUserInfo();
    console.log('After getUserInfo call');
  },

  // 新增方法
  toChangePassword() {
    if (this.checkLoginStatus()) {
      // 跳转到修改密码页面
      wx.navigateTo({
        url: '/pages/changePassword/changePassword',
      })
    }
  },

  toAboutUs() {
    // 关于我们页面可能不需要登录
    wx.navigateTo({
      url: '/pages/aboutUs/aboutUs',
    })
  },

  // 保留其他生命周期函数

  onReady() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}

})